<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\staffapi\controller;


use app\Request;
use app\services\car\CarInfoServices;
use app\services\car\CarOrderDetailServices;
use app\services\car\CarOrderRefundServices;
use app\services\car\CarOrderRerentServices;
use app\services\car\CarOrderReturnServices;
use app\services\car\CarOrderServices;
use app\services\car\CarOrderTakeServices;
use app\services\car\CarStaffService;
use app\services\car\CarTypeServices;
use app\services\car\CarUpkeepServices;
use think\facade\Db;
class StaffController
{
    protected $services = null;

    /**
     * StaffController constructor.
     * @param CarStaffService $services
     */
    public function __construct(CarStaffService $services){
        $this->services = $services;
    }

    /**
     * 保养列表
     * @param Request $request
     * @param CarInfoServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUpkeepCar(Request $request,CarInfoServices $services){
        $manager =  $request->managerInfo();
        return app('json')->success($services->getUpkeepCar($manager['place_id']));
    }

    /**
     * 获取门店近期年检车辆列表及数量
     * @param Request $request
     * @param CarInfoServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAnnualCar(Request $request,CarInfoServices $services){
        $manager =  $request->managerInfo();
        return app('json')->success($services->getAnnualCar($manager['place_id']));
    }

    /**
     * 获取门店近期商业险车辆列表及数量
     * @param Request $request
     * @param CarInfoServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCommercialInsuranceCar(Request $request,CarInfoServices $services){
        $manager =  $request->managerInfo();
        return app('json')->success($services->getCommercialInsuranceCar($manager['place_id']));
    }

    /**
     * 获取门店近期强险车辆列表及数量
     * @param Request $request
     * @param CarInfoServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInsuranceCar(Request $request,CarInfoServices $services){
        $manager =  $request->managerInfo();
        return app('json')->success($services->getInsuranceCar($manager['place_id']));
    }

    /**
     * 获取车辆详情
     * @param Request $request
     * @param CarInfoServices $services
     * @return mixed
     */
    public function getCarDetail(Request $request,CarInfoServices $services){
        $data = $request->getMore([
            ['car_id','']
        ]);
        return app('json')->success($services->getCarDetail((int)$data['car_id']));
    }

    /**
     * 提交保养表单
     * @param Request $request
     * @param CarUpkeepServices $services
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function submitUpkeepData(Request $request,CarUpkeepServices $services){
        $data = $request->postMore([
            ['car_id',''],
            ['upkeep_km',''],
            ['upkeep_date',''],
            ['remark','']
        ]);
        if(!$data['car_id']) return app('json')->fail('缺少参数');
        if(!$data['upkeep_km']) return app('json')->fail('请输入保养时公里数');
        if(!$data['upkeep_date']) return app('json')->fail('请选择保养日期');
        $manager = $request->managerInfo();
        return app('json')->success($services->submitUpkeepData($data,$manager,2));
    }

    /**
     * 订单列表
     * @param Request $request
     * @param CarOrderServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderList(Request $request,CarOrderServices $services){
        $manager =  $request->managerInfo();
        $where = $request->postMore([
            ['place_id',$manager['place_id']],
            ['status',''],
            ['keyword',''],
            ['field_key','order_no']
        ]);
        return app('json')->success($services->getList($where,true,'staff_side'));
    }

    /**
     * 获取最新订单
     * @param Request $request
     * @return mixed
     */
    public function get_order_status(Request $request){
        $manager =  $request->managerInfo();
        //查询最新订单状态
        $check_order = Db::name('car_order')->field('id,status')->where('store_id',$manager['place_id'])->where('status','<>','0')->group('status')->order('status desc')->select()->toArray();
        if($check_order){
            $arr = array_column($check_order,'status');
            $arr2 = ['10','20','30','40','-1'];
            foreach($arr as $kk=>$vv){
                if(in_array('10',$arr2)){
                    $order_status = 10;
                    break;
                }elseif(in_array('20',$arr2)){
                    $order_status = 20;
                    break;
                }elseif(in_array('30',$arr2)){
                    $order_status = 30;
                    break;
                }elseif(in_array('40',$arr2)){
                    $order_status = 40;
                    break;
                }elseif(in_array('-1',$arr2)){
                    $order_status = -1;
                    break;
                }
            }
        }else{
            $order_status = 10;
        }
        return app('json')->success(['order_status'=>$order_status]);

    }

    /**
     * 订单详情
     * @param Request $request
     * @param CarOrderServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderDetail(Request $request,CarOrderServices $services){
        $data = $request->getMore([
            ['order_id',''],
        ]);
        if(!$data['order_id']) return app('json')->fail('参数错误');
        return app('json')->success($services->getOrderInfo((int)$data['order_id'],'staff_side'));
    }

    /**
     * 订单备注
     * @param Request $request
     * @param CarOrderServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderRemark(Request $request,CarOrderServices $services){
        $data = $request->postMore([
            ['order_id',''],
            ['remark','']
        ]);
        if(!$data['order_id']) return app('json')->fail('参数错误');
        if(!$data['remark']) return app('json')->fail('请输入备注');
        return app('json')->success($services->orderRemark($data));
    }

    /**
     * 取消订单
     * @param Request $request
     * @param CarOrderServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function orderCancel(Request $request,CarOrderServices $services){
        $data = $request->postMore([
            ['order_id','']
        ]);
        if(!$data['order_id']) return app('json')->fail('参数错误');
        $staffInfo = $request->managerInfo();
        return app('json')->success($services->orderCancel((int)$data['order_id'],$staffInfo,2));
    }

    /**
     * 创建当面收费
     * @param Request $request
     * @param CarOrderDetailServices $services
     * @return \think\Response
     */
    public function singleFaceFee(Request $request,CarOrderDetailServices $services){
        $data = $request->postMore([
            ['order_id',''],
            ['fee_type',''],
            ['fee_name',''],
            ['price',''],
            ['remark',''],
            ['voucher_pic',''],
        ]);
        if(!$data['order_id']) return app('json')->fail('参数错误');
        if(!$data['fee_type']) return app('json')->fail('请选择扣款类型');
        if(!$data['price']) return app('json')->fail('请输入扣款金额');
        if(!$data['remark']) return app('json')->fail('为避免纠纷，请输入收费说明');
        $staffInfo = $request->managerInfo();
        return app('json')->success($services->singleFaceFee($data,$staffInfo,2));
    }

    /**
     * 创建扣款
     * @param Request $request
     * @param CarOrderDetailServices $services
     * @return \think\Response
     */
    public function deductMoneyFee(Request $request,CarOrderDetailServices $services){
        $data = $request->postMore([
            ['order_id',''],
            ['fee_type',''],
            ['fee_name',''],
            ['price',''],
            ['remark',''],
            ['voucher_pic',''],
        ]);
        if(!$data['order_id']) return app('json')->fail('参数错误');
        if(!$data['fee_type']) return app('json')->fail('请选择扣款类型');
        if(!$data['price']) return app('json')->fail('请输入扣款金额');
        $staffInfo = $request->managerInfo();
        return app('json')->success($services->deductMoneyFee($data,$staffInfo,2));
    }

    /**
     * 获取退款订单
     * @param Request $request
     * @param CarOrderServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderRefund(Request $request,CarOrderServices $services){
        $data = $request->getMore([
            ['order_id',0],
        ]);
        return app('json')->success($services->getOrderRefund((int)$data['order_id']));
    }

    /**
     * 获取续租退款订单
     * @param Request $request
     * @param CarOrderServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRefundRerent(Request $request,CarOrderServices $services){
        $data = $request->getMore([
            ['rerent_id',0],
        ]);
        return app('json')->success($services->getRerentRefund((int)$data['rerent_id']));
    }

    /**
     * 获取退款订单
     * @param Request $request
     * @param CarOrderRefundServices $services
     * @return \think\Response
     */
    public function getRefundList(Request $request,CarOrderRefundServices $services){
        $data = $request->getMore([
            ['place_id',0],
            ['order_type',1],
        ]);
        return app('json')->success($services->getList($data,true));
    }

    /**
     * 创建续租订单
     * @param Request $request
     * @param CarOrderRerentServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function createRerent(Request $request,CarOrderRerentServices $services){
        $data = $request->postMore([
            ['order_id',''],
            ['date',''],
            ['is_occupy',0],// 判断是否强制续租 0询问是否强制续租，10强制续租
        ]);
        if(!$data['order_id']) app('json')->fail('参数错误');
        if(!$data['date']) app('json')->fail('请选择续期还车时间');
        $staffInfo = $request->managerInfo();
        return app('json')->success($services->createRerent($data,$staffInfo,2));
    }

    /**
     * 订单续租列表
     * @param Request $request
     * @param CarOrderRerentServices $services
     * @return \think\Response
     */
    public function getRerentList(Request $request,CarOrderRerentServices $services){
        $data = $request->getMore([
            ['order_id',''],
            ['rerent_status','']
        ]);
        if(!$data['order_id']) return app('json')->fail('参数错误');
        return app('json')->success($services->getList($data,true));
    }

    /**
     * 续租订单详情
     * @param Request $request
     * @param CarOrderRerentServices $services
     * @return \think\Response
     */
    public function getRerentDetail(Request $request,CarOrderRerentServices $services){
        $data = $request->getMore([
            ['rerent_id',''],
        ]);
        if(!$data['rerent_id']) return app('json')->fail('参数错误');
        return app('json')->success($services->getRerentInfo((int)$data['rerent_id']));
    }

    /**
     * 取消续租
     * @param Request $request
     * @param CarOrderRerentServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cancelRerent(Request $request,CarOrderRerentServices $services){
        $data = $request->postMore([
            ['rerent_id','']
        ]);
        if(!$data['rerent_id']) return app('json')->fail('参数错误');
        $staffInfo = $request->managerInfo();
        return app('json')->success($services->cancelRerent((int)$data['rerent_id'],$staffInfo,2));
    }

    /**
     * 创建还车信息
     * @param Request $request
     * @param CarOrderReturnServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function accomplishReturn(Request $request,CarOrderReturnServices $services){
//        $json=json_decode(file_get_contents('php://input'), true);
        $data = $request->postMore([
            ['return_time',''],
            ['return_pic_oil',''],
//            ['return_injury_item',''],
//            ['return_injury_pic',''],
            ['return_oil_fee',''],
            ['return_video',''],
            ['order_id',''],
            ['return_km',''],
            ['ss_array',''],//损坏原因、金额、图片
            ['ss_car_price',0],//损失总金额金额
            ['is_yq','0'],//0不逾期 0.5逾期天数 1逾期天数 以此类推
            ['is_tq','0'],//提前天数
            ['type',''],//yuqi   tiqian
        ]);
//        $array = json_encode($data['ss_array'],JSON_UNESCAPED_UNICODE);
//        $array = json_encode($json,JSON_UNESCAPED_UNICODE);
        $data["ss_car_array"] = json_encode($data["ss_array"],JSON_UNESCAPED_UNICODE);
        if(!$data['return_pic_oil']) return app('json')->fail('请上传还车油表图');
        if(!$data['return_video']) return app('json')->fail('请上传还车验车视频');
        if(!$data['return_time']) return app('json')->fail('请选择实际还车时间');
        if(!$data['return_km']) return app('json')->fail('请输入还车时公里数');
        if($data['ss_car_price']<0)return app('json')->fail('车损金额不能小于0！');
        $staffInfo = $request->managerInfo();
        return app('json')->success($services->accomplishReturn($data,$staffInfo,2));
    }

    /**
     * 获取逾期弹框信息
     * @param Request $request
     * @param CarOrderReturnServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_yuqi_data(Request $request,CarOrderReturnServices $services){
        $data = $request->postMore([
            ['ss_car_price',0],//损失总金额金额
            ['order_id',''],
            ['type',''],
            ['is_yq',0],//0不逾期 0.5逾期天数 1逾期天数 以此类推
            ['is_tq',0],//提前还车天数
        ]);
        $staffInfo = $request->managerInfo();
        return app('json')->success($services->get_yuqi_data($data,$staffInfo,2));
    }

    /**
     * 获取最大逾期天数/提前还车天数
     * @param Request $request
     * @param CarOrderReturnServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_yq_tq_day(Request $request,CarOrderReturnServices $services){
        $data = $request->postMore([
            ['order_id',''],
        ]);
        $staffInfo = $request->managerInfo();
        return app('json')->success($services->get_yq_tq_day($data,$staffInfo,2));
    }
    /**
     * 创建取车信息
     * @param Request $request
     * @param CarOrderTakeServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function accomplishTake(Request $request,CarOrderTakeServices $services){
        $data = $request->postMore([
            ['car_id',0],
            ['car_no',''],
            ['take_time',''],
            ['take_pic_oil',''],
            ['take_video',''],
            ['take_injury_pic',''],
            ['take_contract_pic',''],
            ['old_type_id',0],
            ['new_type_id',0],
            ['order_id',0],
            ['integral_fee',0],
            ['deposit_fee',0],
            ['deposit_pay_time',0],
            ['sign_post_time',0],
            ['sign_notify_time',0],
            ['take_km', '']
        ]);
        if(!$data['car_no']) return app('json')->fail('请选择车车辆');
        if(!$data['take_time']) return app('json')->fail('请选择取车时间');
        if(!$data['take_pic_oil']) return app('json')->fail('请上传取车油表里程图片');
        if(!$data['take_video']) return app('json')->fail('请上传验车视频');
        if(!$data['take_contract_pic']) return app('json')->fail('请上传租车合同');
        if(!$data['take_km']) return app('json')->fail('请输入取车时公里数');
        $staffInfo = $request->managerInfo();
        return app('json')->success($services->accomplisTake($data,$staffInfo,2));
    }

    /**
     * 排车
     * @param Request $request
     * @param CarTypeServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function platoonCar(Request $request,CarTypeServices $services){
        $data = $request->getMore([
            ['order_id',''],
            ['type_id',''],
            ['start_time',''],
            ['end_time',''],
        ]);
        if(!$data['type_id']) return app('json')->fail('参数错误');
        return app('json')->success($services->platoonCar($data));
    }

    /**
     * 获取门店车型
     * @param Request $request
     * @param CarTypeServices $services
     * @return \think\Response
     */
    public function storeCarList(Request $request,CarTypeServices $services){
        $data = $request->getMore([
            ['store_id',0]
        ]);
        if(!$data['store_id']) return app('json')->fail('参数错误');
        return app('json')->success($services->storeCar((int)$data['store_id']));
    }

    /**
     * 换车保存
     * @param Request $request
     * @param CarTypeServices $services
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function changeSaveCar(Request $request,CarTypeServices $services){
        $data = $request->postMore([
            ['order_id',0],
            ['type_name',''],
            ['type_id',0],
            ['pic',''],
            ['price',''],
        ]);
        if(!$data['order_id']) return app('json')->fail('参数错误');
        $staffInfo = $request->managerInfo();
        return app('json')->success($services->changeSaveCar($data,$staffInfo,2));
    }
}
